from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep

# chromedriver版本为137版本
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

def add_product(product_data):
    # 顶部导航
    browser.find_element(By.ID, "mainmenutd_products").click()
    browser.find_element(By.CSS_SELECTOR, '.menu_contenu_product_card .vsmenu').click()

    # 产品编号
    browser.find_element(By.ID, "ref").send_keys(product_data["ref"])
    # 标签
    browser.find_element(By.ID, "label").send_keys(product_data["label"])

    # 是否可销售（固定）
    browser.find_element(By.CSS_SELECTOR,
                         ".centpercent>tbody>tr:nth-child(3)>td:nth-child(2) .select2-container").click()
    WebDriverWait(browser, 3).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR,
                                    ".select2-results__options>li:nth-child(1)"))
    ).click()

    # 是否可采购（固定）
    browser.find_element(By.CSS_SELECTOR, "#select2-statut_buy-container").click()
    WebDriverWait(browser, 3).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR,
                                    ".select2-results__options > li:nth-child(1)"))
    ).click()

    # 使用批号/序列号（固定写2）
    browser.find_element(By.CSS_SELECTOR,
                         "select#status_batch > option[value='2']").click()

    # 销售日期or保质期
    browser.find_element(By.CSS_SELECTOR,
                         "select#sell_or_eat_by_mandatory > option[value='1']").click()

    # 描述
    browser.find_element(By.ID, 'desc').send_keys(product_data["desc"])
    # 公开网址
    browser.find_element(By.CLASS_NAME, 'quatrevingtpercent').send_keys(product_data["url"])

    # 默认仓库
    browser.find_element(By.CSS_SELECTOR,
                         f"select#fk_default_warehouse > option:nth-child(2)").click()

    # 库存信息
    browser.find_element(By.NAME, "seuil_stock_alerte").send_keys(str(product_data["stock_alert"]))
    browser.find_element(By.NAME, "desiredstock").send_keys(str(product_data["desired_stock"]))

    # 重量和单位(克、吨、公斤、吨)
    browser.find_element(By.NAME, "weight").send_keys(str(product_data["weight"]))
    browser.find_element(By.XPATH,
                         f"//select[@id='weight_units']/option[text()='{product_data['weight_unit']}']").click()

    # 尺寸
    browser.find_element(By.NAME, "size").send_keys(str(product_data["length"]))
    browser.find_element(By.NAME, "sizewidth").send_keys(str(product_data["width"]))
    browser.find_element(By.NAME, "sizeheight").send_keys(str(product_data["height"]))

    # 面积 & 体积
    browser.find_element(By.NAME, "surface").send_keys(str(product_data["surface"]))
    browser.find_element(By.NAME, "volume").send_keys(str(product_data["volume"]))

    # 海关代码等（value=9不许改）
    browser.find_element(By.NAME, "customcode").send_keys(product_data["customcode"])
    browser.find_element(By.CSS_SELECTOR,
                         "select#selectcountry_id > option[value='9']").click()

    # 备注
    browser.find_element(By.ID, "note_private").send_keys(product_data["note"])

    # 售价
    browser.find_element(By.NAME, "price").send_keys(str(product_data["price"]))
    browser.find_element(By.NAME, "price_min").send_keys(str(product_data["price_min"]))

    # 会计科目
    browser.find_element(By.NAME, "accountancy_code_sell").send_keys(product_data["acc_sell"])
    browser.find_element(By.NAME, "accountancy_code_sell_export").send_keys(product_data["acc_sell_export"])
    browser.find_element(By.NAME, "accountancy_code_buy").send_keys(product_data["acc_buy"])

    # 产品性质
    browser.find_element(By.CSS_SELECTOR,
                         f"select#finished > option[value='{product_data['product_type']}']").click()

    browser.find_element(By.NAME, "accountancy_code_buy_export").send_keys(product_data["acc_buy_export"])

    # 创建按钮
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        # 测试数据（4正向+4反向，共8个用例）
        #测试结果：正向全部通过，反向，第一个提示为空，最后一个提示找不到单位即元素
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "ref": "PROD001",
                "label": "夏季新款T恤",
                "desc": "纯棉材质，圆领短袖，白色基础款，适合日常休闲穿搭",
                "url": "https://www.example.com/products/PROD001",
                "stock_alert": 10,
                "desired_stock": 50,
                "weight": 200,
                "weight_unit": "克",
                "length": 700,
                "width": 500,
                "height": 5,
                "surface": 700,
                "volume": 1750000,
                "customcode": "001",
                "note": "常规商品",
                "price": 59.9,
                "price_min": 49.9,
                "acc_sell": "600101",
                "acc_sell_export": "600201",
                "acc_buy": "140301",
                "acc_buy_export": "140302",
                "product_type": "1"
            },
            {
                "ref": "PROD002",
                "label": "5G智能手机",
                "desc": "6.7英寸屏幕，128GB存储，5000mAh电池，支持5G网络",
                "url": "https://www.example.com/products/PROD002",
                "stock_alert": 5,
                "desired_stock": 20,
                "weight": 200,
                "weight_unit": "克",
                "length": 160,
                "width": 75,
                "height": 9,
                "surface": 12000,
                "volume": 108000,
                "customcode": "851712",  # 手机海关编码
                "note": "跨境商品，需海关备案",
                "price": 2999.9,
                "price_min": 2799.9,
                "acc_sell": "600103",
                "acc_sell_export": "600203",
                "acc_buy": "140303",
                "acc_buy_export": "140304",
                "product_type": "1"
            },
            {
                "ref": "PROD003",
                "label": "比利时进口巧克力",
                "desc": "72%可可含量，100g/盒，生产日期2025-05-01",
                "url": "https://www.example.com/products/PROD003",
                "stock_alert": 20,
                "desired_stock": 100,
                "weight": 100,
                "weight_unit": "克",
                "length": 150,
                "width": 100,
                "height": 50,
                "surface": 15000,
                "volume": 750000,
                "customcode": "180631",  # 巧克力海关编码
                "note": "需冷藏保存，保质期12个月",
                "price": 89.9,
                "price_min": 79.9,
                "acc_sell": "600102",
                "acc_sell_export": "600202",
                "acc_buy": "140302",
                "acc_buy_export": "140303",
                "product_type": "1"
            },
            {
                "ref": "PROD004",
                "label": "布艺沙发",
                "desc": "L型三人位，灰色布艺，实木框架，尺寸240*180*85cm",
                "url": "https://www.example.com/products/PROD004",
                "stock_alert": 3,
                "desired_stock": 10,
                "weight": 50000,  # 50kg
                "weight_unit": "克",
                "length": 2400,
                "width": 1800,
                "height": 850,
                "surface": 4320000,
                "volume": 3672000000,
                "customcode": "940310",  # 沙发海关编码
                "note": "大件商品，需预约配送",
                "price": 2999.0,
                "price_min": 2799.0,
                "acc_sell": "600104",
                "acc_sell_export": "600204",
                "acc_buy": "140304",
                "acc_buy_export": "140305",
                "product_type": "1"
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "ref": "",  # 空编号
                "label": "测试产品",
                "desc": "反向测试用例",
                "url": "https://www.example.com/test",
                "stock_alert": 10,
                "desired_stock": 50,
                "weight": 100,
                "weight_unit": "克",
                "length": 100,
                "width": 100,
                "height": 10,
                "surface": 10000,
                "volume": 100000,
                "customcode": "001",
                "note": "测试",
                "price": 99.9,
                "price_min": 89.9,
                "acc_sell": "600101",
                "acc_sell_export": "600201",
                "acc_buy": "140301",
                "acc_buy_export": "140302",
                "product_type": "1",
                "expected_result": "添加失败，提示\"产品编号不能为空\""
            },
            {
                "ref": "PROD005",
                "label": "非法价格产品",
                "desc": "测试负价格输入",
                "url": "https://www.example.com/illegal",
                "stock_alert": 10,
                "desired_stock": 50,
                "weight": 100,
                "weight_unit": "克",
                "length": 100,
                "width": 100,
                "height": 10,
                "surface": 10000,
                "volume": 100000,
                "customcode": "001",
                "note": "测试",
                "price": -99.9,  # 负价格
                "price_min": -89.9,
                "acc_sell": "600101",
                "acc_sell_export": "600201",
                "acc_buy": "140301",
                "acc_buy_export": "140302",
                "product_type": "1",
                "expected_result": "添加失败，提示\"价格不能为负数\""
            },
            {
                "ref": "A" * 31,  # 31位超长编号
                "label": "超长编号产品",
                "desc": "测试系统对超长编号的处理",
                "url": "https://www.example.com/long-ref",
                "stock_alert": 10,
                "desired_stock": 50,
                "weight": 100,
                "weight_unit": "克",
                "length": 100,
                "width": 100,
                "height": 10,
                "surface": 10000,
                "volume": 100000,
                "customcode": "001",
                "note": "测试",
                "price": 99.9,
                "price_min": 89.9,
                "acc_sell": "600101",
                "acc_sell_export": "600201",
                "acc_buy": "140301",
                "acc_buy_export": "140302",
                "product_type": "1",
                "expected_result": "添加失败，提示\"产品编号长度不能超过30位\""
            },
            {
                "ref": "PROD006",
                "label": "无效单位产品",
                "desc": "测试非法重量单位输入",
                "url": "https://www.example.com/invalid-unit",
                "stock_alert": 10,
                "desired_stock": 50,
                "weight": 100,
                "weight_unit": "磅",  # 非法单位（仅允许克/吨/公斤）
                "length": 100,
                "width": 100,
                "height": 10,
                "surface": 10000,
                "volume": 100000,
                "customcode": "001",
                "note": "测试",
                "price": 99.9,
                "price_min": 89.9,
                "acc_sell": "600101",
                "acc_sell_export": "600201",
                "acc_buy": "140301",
                "acc_buy_export": "140302",
                "product_type": "1",
                "expected_result": "添加失败，提示\"重量单位无效\""
            }
        ]
        for data in test_data:
            add_product(data)
    except Exception as e:
        print(e)
    finally:
        sleep(1)
        browser.quit()